package com.cloud.wms.ui.inside.revert;

import android.os.Message;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.ui.inside.contract.RevertContact;
import com.cloud.wms.ui.inside.presenter.RevertPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-17 15:12
 * @Description:
 */
public class RevertCommodityActivity extends BaseBarListActivity<CommodityEntity, RevertPresenter> implements RevertContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_wait_revert_amount)
    TextView tvWaitRevertAmount;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected String barTitle() {
        return "还货";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_revert;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_revert_commodity;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        mPresenter.loadRevertCommodity();
        refreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.loadRevertCommodity());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            CommodityEntity commodityBean = mAdapter.getData().get(i);
            if (commodityBean.matchBarCode(result)) {
//            if (result.equals(commodityBean.getCommodityName())) {
                success("扫描商品验证成功！");
                RevertPosActivity.start(RevertCommodityActivity.this, commodityBean, true);
                return;
            }
        }

        mPresenter.checkCommodity(result);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, CommodityEntity item) {
        helper.setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_amount, item.getAmount() + "");
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case RevertPresenter.GET_REVERT_COMMODITY:
                refreshLayout.finishRefresh();
                mAdapter.setNewInstance(((ArrayList<CommodityEntity>) message.obj));
                break;
            case RevertPresenter.GET_REVERT_COMMODITY_AMOUNT:
                tvWaitRevertAmount.setText(message.obj.toString());
                break;
            case RevertPresenter.CHECK_REVERT_COMMODITY:
                onAlert("当前商品属于多拣，是否进行还货？", (dialog, which) -> {
                    RevertPosActivity.start(RevertCommodityActivity.this, ((CommodityEntity) message.obj), false);
                });
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                mPresenter.loadRevertCommodity();
                break;
        }
    }
}
